﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Net.Mail;

/// <summary>
/// Summary description for ProfileService
/// </summary>
public class ScheduleServiceImpl : IScheduleService {
	public ScheduleServiceImpl() { }

	public void saveDestination(Destination destination) {
		try {
			IDestinationDAO destinationDAO = AbstractDAOFactory.getInstance(AbstractDAOFactory.Type.MySQL).createDestinationDAO();
			destinationDAO.saveDestination(destination);
		} catch (PersistenceException e) {
			throw new ServiceException("Failed to save destination information", e);
		}
	}

	public void saveSchedules(User user) {
		//Validate that departure times are after arrival times
		BusinessException be = new BusinessException();
		foreach (Schedule schedule in user.Schedules) {
			if (schedule.ReturnHour >= schedule.DepartureHour) {
				be.addMessage(new Message(Message.Type.ERROR, "Departure time for " + schedule.DayOfWeek.ToString() + " must be after arrival time"));
			}
		}
		if (be.Messages.Count > 0) {
			throw be;
		}

		try {
			IScheduleDAO scheduleDAO = AbstractDAOFactory.getInstance(MySQLDAOFactory.Type.MySQL).createScheduleDAO();
			foreach (Schedule schedule in user.Schedules) {
				scheduleDAO.saveSchedule(schedule);
			}
		} catch (PersistenceException e) {
			throw new ServiceException("Failed to save schedule information", e);
		}
	}

	public List<Schedule> findSchedules(ScheduleSearchCriteria criteria) {
		List<Schedule> result = new List<Schedule>();
		IScheduleDAO scheduleDAO = AbstractDAOFactory.getInstance(AbstractDAOFactory.Type.MySQL).createScheduleDAO();
		List<Schedule> tempSchedules;
		try {
			tempSchedules = scheduleDAO.readSchedules(criteria);

			//Strip out schedules with the same user ID
			foreach (Schedule queriedSchedule in tempSchedules) {
				if (queriedSchedule.User.Id != criteria.thisUser.Id) {
					result.Add(queriedSchedule);
				}
			}
		} catch (PersistenceException e) {
			throw new ServiceException("Failed to find schedules", e);
		}

		return result;
	}

	public List<Schedule> findCompatibleSchedules(User paramUser) {
		List<Schedule> result = new List<Schedule>();
		try {
			IScheduleDAO scheduleDAO = AbstractDAOFactory.getInstance(AbstractDAOFactory.Type.MySQL).createScheduleDAO();
			ScheduleSearchCriteria scheduleCriteria = new ScheduleSearchCriteria();
			scheduleCriteria.Zip = paramUser.Destinations[0].Zip;
			foreach (Schedule schedule in paramUser.Schedules) {
				scheduleCriteria.DayOfWeek = schedule.DayOfWeek;
				scheduleCriteria.DepartureHour = schedule.DepartureHour;
				scheduleCriteria.ReturnHour = schedule.ReturnHour;

				//Strip out schedules with the same user ID
				List<Schedule> tempSchedules = scheduleDAO.readSchedules(scheduleCriteria);
				foreach (Schedule queriedSchedule in tempSchedules) {
					if (queriedSchedule.User.Id != paramUser.Id) {
						result.Add(queriedSchedule);
					}
				}
			}
		} catch (PersistenceException e) {
			throw new ServiceException("Failed to find compatible schedules", e);
		}
		return result;
	}

	public void contactUser(User fromUser, User toUser, string messageBody) {
		MailMessage mailMessage = new MailMessage(fromUser.Email, toUser.Email);
		mailMessage.Subject = "Carpool Finder - Message from " + fromUser.FirstName + " " + fromUser.LastName;
		mailMessage.Body = messageBody;
		new SmtpClient("localhost").Send(mailMessage);
	}
}
